﻿using ETA.DtoMappings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Threading;
using System.Net;
using System.IO;
using ETA.Common;
using System.Text;

namespace ETA.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleTable.EnableOptimizations = false;
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            DtoMapConfig.InitMap();
            Speed();
        }

        /// <summary>
        /// 解决Nhibernate 初始化速度问题
        /// </summary>
        private void Speed()
        {
            Logger.Log.Info("进程开启");
            try
            {
                System.Timers.Timer timer = new System.Timers.Timer();
                timer.Elapsed += (source, args) =>
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Config.LoadUrl);
                    request.Method = "GET";
                    request.ContentType = "text/html;charset=UTF-8";
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream myResponseStream = response.GetResponseStream();
                    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                    string retString = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                    myResponseStream.Close();
                    Logger.Log.Info(retString);
                };
                timer.Interval = Config.LoadInterval; //*60 * 10;
                timer.Enabled = true;
            }
            catch (Exception e)
            {
                Logger.Log.Error(e.Message);
            }
        }
    }
}